package com.kede.app.service.impl;

import com.kede.app.domain.ZhiGoodValid;
import com.kede.utils.ValidationUtil;
import com.kede.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.kede.app.repository.ZhiGoodValidRepository;
import com.kede.app.service.ZhiGoodValidService;
import com.kede.app.service.dto.ZhiGoodValidDto;
import com.kede.app.service.dto.ZhiGoodValidQueryCriteria;
import com.kede.app.service.mapstruct.ZhiGoodValidMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.kede.utils.PageUtil;
import com.kede.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @description 服务实现
* @author Pr.li
* @date 2023-11-04
**/
@Service
@RequiredArgsConstructor
public class ZhiGoodValidServiceImpl implements ZhiGoodValidService {

    private final ZhiGoodValidRepository zhiGoodValidRepository;
    private final ZhiGoodValidMapper zhiGoodValidMapper;

    @Override
    public Map<String,Object> queryAll(ZhiGoodValidQueryCriteria criteria, Pageable pageable){
        Page<ZhiGoodValid> page = zhiGoodValidRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(zhiGoodValidMapper::toDto));
    }

    @Override
    public List<ZhiGoodValidDto> queryAll(ZhiGoodValidQueryCriteria criteria){
        return zhiGoodValidMapper.toDto(zhiGoodValidRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ZhiGoodValidDto findById(Integer id) {
        ZhiGoodValid zhiGoodValid = zhiGoodValidRepository.findById(id).orElseGet(ZhiGoodValid::new);
        ValidationUtil.isNull(zhiGoodValid.getId(),"ZhiGoodValid","id",id);
        return zhiGoodValidMapper.toDto(zhiGoodValid);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ZhiGoodValidDto create(ZhiGoodValid resources) {
        return zhiGoodValidMapper.toDto(zhiGoodValidRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ZhiGoodValid resources) {
        ZhiGoodValid zhiGoodValid = zhiGoodValidRepository.findById(resources.getId()).orElseGet(ZhiGoodValid::new);
        ValidationUtil.isNull( zhiGoodValid.getId(),"ZhiGoodValid","id",resources.getId());
        zhiGoodValid.copy(resources);
        zhiGoodValidRepository.save(zhiGoodValid);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            zhiGoodValidRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ZhiGoodValidDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ZhiGoodValidDto zhiGoodValid : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("店铺名称", zhiGoodValid.getName());
            map.put("通过数量", zhiGoodValid.getPass());
            map.put("失败数量", zhiGoodValid.getFail());
            map.put("验收数量", zhiGoodValid.getNum());
            map.put("门店id", zhiGoodValid.getStoreId());
            map.put("应收金额", zhiGoodValid.getShouldMoney());
            map.put(" createTime",  zhiGoodValid.getCreateTime());
            map.put("区代id", zhiGoodValid.getQuId());
            map.put("市代id", zhiGoodValid.getShiId());
            map.put("运输单id", zhiGoodValid.getTransportId());
            map.put("实收金额", zhiGoodValid.getRealMoney());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}